package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;


    //@RequiredPermission(name = "部门列表",expression = "department:list")
    //@RequiresRoles("ADMIN")
    @RequiresPermissions(value = {"department:list","部门列表"},logical= Logical.OR)
    @RequestMapping("/list")
    public String listAll(Model model,QueryObject qo){
        PageInfo<Department> pageResult = departmentService.query(qo);
        model.addAttribute("pageResult",pageResult);
        //因为配了 前缀和后缀
        return "department/list";
    }

    //处理删除请求
    @RequestMapping("/delete")
    @ResponseBody
    //@RequiredPermission(name = "部门删除",expression = "department:delete")
    public JsonResult delete(Long id){
        try {
            //int a = 1/0;
            if (id != null){
                departmentService.delete(id);
            }
            return new JsonResult(true,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
    }
    //保存请求
   /* @RequestMapping("/input")
    public String input(Long id,Model model){
        if (id != null){
            model.addAttribute("department",
                    departmentService.select(id));
        }
        return "department/input";
    }*/
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department department) {
        try {
            if (department.getId() != null) {
                departmentService.update(department);
            } else {
                departmentService.save(department);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "保存失败");
        }
    }
}
